//JPTTPlugin.java
package qwq.vincentzyu;

import org.bukkit.Location;
import org.bukkit.plugin.java.JavaPlugin;
import qwq.vincentzyu.Command.EggReturnCommand;
import qwq.vincentzyu.Command.SnowballKillCommand;
import qwq.vincentzyu.Event.SnowballKillListener;

public class JPTTPlugin extends JavaPlugin {

    private boolean isSnowballKillEnabled = true;
    private boolean isEggReturnEnabled = true;
    private Location returnLocation;

    public boolean isSnowballKillEnabled(){
        return isSnowballKillEnabled;
    }

    public void setSnowballKillEnabled(boolean snowballKillEnabled) {
        isSnowballKillEnabled = snowballKillEnabled;
        getConfig().set("snowball_kill.enabled", isSnowballKillEnabled);
        saveConfig();
    }

    public boolean isEggReturnEnabled() {
        return isEggReturnEnabled;
    }

    public void setEggReturnEnabled(boolean eggReturnEnabled) {
        isEggReturnEnabled = eggReturnEnabled;
    }

    public Location getReturnLocation() {
        return returnLocation;
    }

    public void setReturnLocation(Location returnLocation) {
        this.returnLocation = returnLocation;
    }

    public void updateEggConfig(boolean enabled, double... coords){
        isEggReturnEnabled = enabled;
        if (coords.length >= 3){
            returnLocation.setX(coords[0]);
            returnLocation.setY(coords[1]);
            returnLocation.setZ(coords[2]);
        }
        if (coords.length==5){
            returnLocation.setPitch((float) coords[3]);
            returnLocation.setYaw((float) coords[4]);
        }

        getConfig().set("egg_return.enabled", enabled);
        getConfig().set("egg_return.pos.x", returnLocation.getX());
        getConfig().set("egg_return.pos.y", returnLocation.getY());
        getConfig().set("egg_return.pos.z", returnLocation.getZ());
        getConfig().set("egg_return.pos.pitch", returnLocation.getPitch());
        getConfig().set("egg_return.pos.yaw", returnLocation.getYaw());

        saveConfig();
    }

    @Override
    public void onEnable() {
        saveDefaultConfig();
        isSnowballKillEnabled = getConfig().getBoolean("snowball_kill.enabled");
        isEggReturnEnabled = getConfig().getBoolean("egg_return.enabled");
        returnLocation = new Location(
            getServer().getWorlds().get(0), // 默认主世界
            getConfig().getDouble("egg_return.pos.x", 0),
            getConfig().getDouble("egg_return.pos.y", 0),
            getConfig().getDouble("egg_return.pos.z", 0),
            (float) getConfig().getDouble("egg_return.pos.pitch", 0),
            (float) getConfig().getDouble("egg_return.pos.yaw", 0)
        );

        getCommand("qwq_snowball_kill").setExecutor(new SnowballKillCommand(this));
        getCommand("qwq_egg_return").setExecutor(new EggReturnCommand(this));
        getServer().getPluginManager().registerEvents(new SnowballKillListener(this), this);
    }

    @Override
    public void onDisable() {
        getConfig().set("snowball_kill_enabled", isEnabled());
        saveConfig();
    }

}
